//package com.djt.book_system.config;
//
//import com.djt.book_system.service.LoginService;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.security.crypto.password.PasswordEncoder;
//
//import java.io.PrintWriter;
//
//@EnableWebSecurity
//public class SecurityConfig extends WebSecurityConfigurerAdapter {
//
//    @Autowired
//    private LoginService service;
//    @Bean
//    public PasswordEncoder passwordEncoder(){
//        return new BCryptPasswordEncoder();
//    }
//    //重写WebSecurityConfigurerAdapter类中的configure方法，
//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
////        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
//////              在内存中认证且对密码进行加密
////        auth.inMemoryAuthentication().passwordEncoder(encoder)
////                .withUser("zhangsan").password("123456").roles("1")
////                .and()
////                .withUser("zhangsan").password("123456").roles("1");
//        auth.userDetailsService(service)
//                .passwordEncoder(passwordEncoder());
//
//    }
//
//    @Override
//    protected void configure(HttpSecurity http) throws Exception {
//        http.authorizeRequests()
//                .anyRequest().authenticated()
//                .and()
//                .formLogin()
//                .loginPage("/login")
//                .loginProcessingUrl("/login")
//                .usernameParameter("uname")
//                .passwordParameter("upwd")
//                .successHandler((req , resp , authentication)->{
//                    resp.setContentType("application/json;charset=utf-8");
//                    PrintWriter out = resp.getWriter();
//                    out.write(new ObjectMapper().writeValueAsString(authentication.getPrincipal()));
//                    out.flush();
//                    out.close();
//                })
//                .failureHandler((req , resp , Exception) -> {
//                    PrintWriter out = resp.getWriter();
//                    out.write("");
//                })
//                .permitAll()
//                .and()
//                .logout();
////                .antMatchers("/").permitAll()//开放未登录时首页权限
////                .antMatchers("/index/**","/users/**","/auths/**").hasRole("1")
////                .antMatchers("/index/**","/borrows/**","/books/**").hasRole("2")
////                .antMatchers("/borrows/addOne","/borrows/batchAdd","/books/select").hasRole("3");
//
//    }
//}